package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.pojo.TbEstimate;
import com.workManager.pojo.TdProfit;
import com.workManager.service.EstimateService;
import com.workManager.util.WebUtil;
import com.workManager.vo.CurrentUserVO;
import com.workManager.vo.ProfitVo;
import com.workManager.vo.TbEstimateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/3/3 0003.
 */
@Controller
@RequestMapping("estimate")
public class EstimateController extends BaseController {


    @Autowired
    private EstimateService estimateService;

    @ApiOperation(value ="项目盈利情况查询",notes = "项目盈利情况查询")
    @RequestMapping(value="/queryProfit",method = RequestMethod.POST)
    public Object queryProfit(HttpServletRequest request,
                                  @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                  @RequestParam(value="pageSize", required = false) String pageSize,
                                  @RequestParam(value = "establishDateStart", required = false) String establishDateStart,
                                  @RequestParam(value = "establishDateEnd", required = false) String establishDateEnd,
                                  @RequestParam(value = "singDateStart", required = false) String singDateStart,
                                  @RequestParam(value = "singDateEnd", required = false) String singDateEnd,
                                  @RequestParam(value = "customerName", required = false) String customerName,
                                  @RequestParam(value = "saleName", required = false) String saleName,
                                  @RequestParam(value = "saleOrg", required = false) String saleOrg,
                                  @RequestParam(value = "contractType", required = false) String contractType,
                                  @RequestParam(value = "contractName", required = false) String contractName,
                                  @RequestParam(value = "projectCode", required = false) String projectCode,
                                  @RequestParam(value = "projectName", required = false) String projectName,
                                  @RequestParam(value = "projectId", required = false) String projectId,
                                  @RequestParam(value = "managerName", required = false) String managerName,
                                  @RequestParam(value = "projectStatus", required = false) String projectStatus,
                                  @RequestParam(value = "groupBy", required = false) String groupBy,
                                  @RequestParam(value = "salerOrgId", required = false) String salerOrgId,
                                  @RequestParam(value = "salerId", required = false) String salerId,
                                  @RequestParam(value = "managerId", required = false) String managerId,
                                  @RequestParam(value = "projectIds", required = false) List<Integer> projectIds,
                                  @RequestParam(value = "loginCode", required = false) String loginCode,
                                  @RequestParam(value = "projectType", required = false) String projectType
                                  //@RequestParam(value = "projectIds", required = false) String projectIds


    ){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("loginCode",loginCode);
        map.put("establishDateStart",establishDateStart);
        map.put("establishDateEnd",establishDateEnd);
        map.put("singDateStart",singDateStart);
        map.put("singDateEnd",singDateEnd);
        map.put("customerName",customerName);
        map.put("saleName",saleName);
        map.put("saleOrg",saleOrg);
        map.put("contractType",contractType);
        map.put("contractName",contractName);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("projectId",projectId);
        map.put("managerName",managerName);
        map.put("projectStatus",projectStatus);
        map.put("groupBy",groupBy);
        map.put("salerOrgId",salerOrgId);
        map.put("salerId",salerId);
        map.put("managerId",managerId);
        map.put("loginCode",loginCode);
        map.put("projectType",projectType);
        if(projectIds != null && !"".equals(projectIds)){
            map.put("projectIds",projectIds.toArray());
        }


        PageInfo<ProfitVo> profitPageInfo =  estimateService.queryProfit(map);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",profitPageInfo.getTotal());
        dataMap.put("rows",profitPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value ="查询尚需成本",notes = "查询尚需成本")
    @RequestMapping(value = "/queryNeedCost",method = RequestMethod.POST)
    public Object queryNeedCost(@RequestParam(value = "projectId[]",required = false) List<Integer> projectIds) throws ParseException {
        ModelMap modelMap = new ModelMap();
        Map map = estimateService.queryNeedCost(projectIds);
        return setSuccessModelMap(modelMap,map);
    }

    @ApiOperation(value ="查询实际成本",notes = "查询实际成本")
    @RequestMapping(value = "/queryActualCost",method = RequestMethod.POST)
    public Object queryActualCost(@RequestParam(value = "projectId[]",required = false) List<Integer> projectIds) throws ParseException {
        ModelMap modelMap = new ModelMap();
        Map map = estimateService.queryActualCost(projectIds);
        return setSuccessModelMap(modelMap,map);
    }

    @ApiOperation(value ="保存预估",notes = "保存预估")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Object save(@RequestBody TbEstimate estimate){
        ModelMap modelMap = new ModelMap();
        estimateService.save(estimate);
        return setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="查询预计总成本",notes = "查询预计总成本")
    @RequestMapping(value = "/queryTotalCost",method = RequestMethod.POST)
    public Object queryTotalCost(@RequestParam(value = "projectId[]",required = false) List<Integer> projectIds) throws ParseException {
        ModelMap modelMap = new ModelMap();
        Map map = estimateService.queryTotalCost(projectIds);
        return setSuccessModelMap(modelMap,map);
    }

    @ApiOperation(value ="查询实际利润",notes = "查询实际利润")
    @RequestMapping(value = "/queryActualProfit",method = RequestMethod.POST)
    public Object queryActualProfit(@RequestParam(value = "projectId[]",required = false) List<Integer> projectIds) throws ParseException {
        ModelMap modelMap = new ModelMap();
        Map map = estimateService.queryActualProfit(projectIds);
        return setSuccessModelMap(modelMap,map);
    }

    @ApiOperation(value ="查询预计利润",notes = "查询预计利润")
    @RequestMapping(value = "/queryEstimateProfit",method = RequestMethod.POST)
    public Object queryEstimateProfit(@RequestParam(value = "projectId[]",required = false) List<Integer> projectIds) throws ParseException {
        ModelMap modelMap = new ModelMap();
        Map map = estimateService.queryEstimateProfit(projectIds);
        return setSuccessModelMap(modelMap,map);
    }

}
